import matplotlib.pyplot as plt
from mpmath import  mp,mpf,exp,log, log1p,pi

# 设置多精度浮点数的精度
mp.dps = 50  # 可以根据需要调整精度

# 定义一个使用mpf的函数
def my_log_error_v3(x):
    C0 = mpf("0.0")
    C1 = mpf("1.0")
    C2 = mpf("-0.4999999999943711266184616390431146")
    C3 = mpf("0.3333333229206968314498380186646109")
    C4 = mpf("-0.2499933809613633253457638305496467")
    C5 = mpf("0.1982451827823504344289064653480892")
    mylog = C0 + C1*x  + C2*(x**2) + C3*(x**3) + C4*(x**4) + C5*(x**5)
    return mylog- log1p(x)

# 定义一个使用mpf的函数
def my_log_error_v4(x):
    C0 = mpf("0.0")
    C1 = mpf("1.0")
    C2 = mpf("-0.5")
    C3 = mpf("0.3333333293321563851517852497583704")
    C4 = mpf("-0.2499956856821482508202130687462877")
    C5 = mpf("0.1985102023659856367763140246272085")
    mylog = C0 + C1*x  + C2*(x**2) + C3*(x**3) + C4*(x**4) + C5*(x**5)
    return mylog- log1p(x)


def plot_log_error(mode):

    # 定义x的范围

    k = mpf(1.0)/mpf(256*1000)
    if mode==3 or mode==4:
        x_values = [ mpf(i)*k for i in range(0,1001)]    # 从0到1000的整数

    # 计算对应的y值
    if mode==3:
        y_values = [my_log_error_v3(x) for x in x_values]
    elif mode==4:
        y_values = [my_log_error_v4(x) for x in x_values]

    # 绘制图像
    plt.plot(x_values, y_values)
    plt.xlabel('x')
    plt.ylabel('y')
    if mode==3:
        plt.title('log1p_v3(x)-log1p(x)')
    elif mode==4:
        plt.title('log1p_v4(x)-log1p(x)')

    plt.grid(True)
    plt.show()

plot_log_error(3)
plot_log_error(4)